<i18n>
{
  "en": {
    "page_title": "User Management",
    "input_search_placeholder": "Search",
    "btn_new": "New User",
    "col_username": "User",
    "col_name": "Name",
    "col_role_name": "Role",
    "col_dept": "Department",
    "col_create_time": "Create Time",
    "search_username": "Search User",
    "role_type": "Role Type",
    "select_role": "Select Roles",
    "area": "Area",
    "country": "Country"
  },
  "zh": {
    "page_title": "用户管理",
    "input_search_placeholder": "请输入用户名",
    "btn_new": "新增用户",
    "col_username": "用户名",
    "col_name": "姓名",
    "col_role_name": "角色名称",
    "col_dept": "所属部门",
    "col_create_time": "创建日期",
    "search_username": "搜索用户",
    "role_type": "权限类型",
    "select_role": "选择角色",
    "area": "区域",
    "country": "国家"
  }
}
</i18n>

<template>
  <div>
    <div class="page-header">
      <el-row :gutter="20">
        <el-col :span="8">
          <div class="page-header__title">{{$t('pages.roles.page_title')}}</div>
        </el-col>
        <el-col :span="13" class="text-right">
          <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item>
              <el-input :placeholder="$t('pages.roles.name_placeholder')" v-model="formInline.keyword">
                <i slot="suffix" class="el-input__icon el-icon-search" @click="getDataList()"></i>
              </el-input>
            </el-form-item>
            <el-button type="danger"  @click="search">{{$t('common.search')}}</el-button>
          </el-form>
        </el-col>

        <el-col :span="3" class="text-right">
          <el-button type="primary" icon="el-icon-plus" @click="showAddRoleForm">新增角色</el-button>
        </el-col>
      </el-row>
    </div>

    <el-card class="table-container">
      <el-table :data="dataList">
        <!--序号-->
        <el-table-column prop="id" :label="$t('pages.roles.col_index')"></el-table-column>
        <!--角色名称-->
        <el-table-column prop="name" :label="$t('pages.roles.col_name')"></el-table-column>
        <!--新建者-->
        <el-table-column prop="creater" :label="$t('pages.roles.col_creator')"></el-table-column>
        <!--新建时间-->
        <el-table-column prop="createTm" :label="$t('pages.roles.col_createTm')"></el-table-column>
        <!--操作-->
        <el-table-column
          label-class-name="text-center"
          class-name="text-center action"
          prop="operate"
          :label="$t('common.operation')"
          width="180">
          <template slot-scope="scope">
            <a @click.prevent="allot(scope.row)" class="text-info" href="/">分配</a>
            <router-link class="text-warning" to="/">编辑</router-link>
            <a @click.prevent="rmRole(scope.row)" class="text-danger" href="/">删除</a>
          </template>
        </el-table-column>
      </el-table>

      <!--分页-->
      <el-pagination
        @current-change="onCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="totalPage">
      </el-pagination>
    </el-card>

    <!--新增角色-->
    <el-dialog
      title="新增角色"
      :visible.sync="addDialog"
      width="30%">
      <div>
        <el-form ref="form" :rules="addrule" :model="role" label-width="80px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="role.name"></el-input>
          </el-form-item>
          <el-form-item label="功能权限" prop="resources">
            <el-select v-model="role.resources" placeholder="请选择活动区域">
              <el-option label="区域一" value="shanghai"></el-option>
              <el-option label="区域二" value="beijing"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="okBtn">确 定</el-button>
        <el-button type="danger" @click="addDialog = false">取 消</el-button>
      </span>
    </el-dialog>

    <!--分配角色-->
    <el-dialog
      title="角色分配"
      :visible.sync="allotRoleDialog"
      width="50%">

      <el-form :inline="true" :model="userForm">
        <el-form-item>
          <el-select v-model="userForm.users" filterable placeholder="请搜索用户进行添加">
            <el-option
              v-for="item in searchUsers"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addUserAsRole">分配角色</el-button>
        </el-form-item>
      </el-form>

      <div class="table-container">
        <!--已分配的用户-->
        <el-table :data="roleAsUsers">
          <el-table-column prop="No" label="工号">
          </el-table-column>

          <el-table-column prop="name" label="姓名">
          </el-table-column>

          <el-table-column prop="role" label="所属部门">
          </el-table-column>

          <el-table-column prop="time" label="时间">
          </el-table-column>

          <el-table-column
            class-name="action text-center"
            label-class-name="text-center"
            label="操作">

            <template slot-scope="scope">
              <a href="/" class="text-danger" @click.prevent="rmUserAsRole(scope.row)">移除</a>
            </template>

          </el-table-column>
        </el-table>
      </div>
      <!--<span slot="footer" class="dialog-footer">-->
        <!--<el-button type="primary" @click="addDialog = false">关闭</el-button>-->
        <!--<el-button type="danger" @click="addDialog = false">取 消</el-button>-->
      <!--</span>-->
    </el-dialog>
  </div>
</template>

<script>
  import axios from '@/utils/axios'

  const data = {
    list: [
      {id: 0, name: '技术总监', creater: '张三', createTm: '2018-03-05 12:35'},
      {id: 1, name: '技术总监', creater: '张三', createTm: '2018-03-05 12:35'},
      {id: 2, name: '技术总监', creater: '张三', createTm: '2018-03-05 12:35'},
      {id: 3, name: '技术总监', creater: '张三', createTm: '2018-03-05 12:35'},
      {id: 4, name: '技术总监', creater: '张三', createTm: '2018-03-05 12:35'},
      {id: 5, name: '技术总监', creater: '张三', createTm: '2018-03-05 12:35'}
    ],
    pageSize: 10,
    totalPage: 10,
    currentPage: 2
  }

  export default {
    data () {
      return {
        // 添加角色
        addDialog: false,
        role: {
          name: '',
          resources: ''
        },
        addrule:{
          name: [
            { required: true, message: '角色名称不能为空', trigger: 'blur' }
          ],
          resources: [
            { required: true, message: '功能权限不能为空', trigger: 'change' }
          ]
        },

        // 分配角色
        allotRoleDialog: false,
        roleAsUsers: [
          {id: 0, No: '000', role: '技术总监', name: '张三', time: '2018-03-05 12:35'},
          {id: 1, No: '000', role: '技术总监', name: '张三', time: '2018-03-05 12:35'},
          {id: 2, No: '000', role: '技术总监', name: '张三', time: '2018-03-05 12:35'},
          {id: 3, No: '000', role: '技术总监', name: '张三', time: '2018-03-05 12:35'},
          {id: 4, No: '000', role: '技术总监', name: '张三', time: '2018-03-05 12:35'},
          {id: 5, No: '000', role: '技术总监', name: '张三', time: '2018-03-05 12:35'},
          {id: 6, No: '000', role: '技术总监', name: '张三', time: '2018-03-05 12:35'}
        ],
        rmUserAsRole (user) {
          this.$confirm('是否将当前用户从角色中移除', '移除用户', {
            confirmButtonClass: 'el-button--danger',
            confirmButtonText: '确认',
            cancelButtonText: '取消'
          }).then(() => {
            alert('shanchu ')
          })
        },
        // 添加用户到角色
        addUserAsRole () {},
        searchUsers: [
          {id: 0, name: 'A'},
          {id: 1, name: 'B'},
          {id: 2, name: 'C'}
        ],
        userForm: {
          users: []
        },

        formInline: {
          status: 1,
          keyword: ''
        },
        dataList: data.list || [],
        currentPage: data.currentPage || 1,
        pageSize: data.pageSize || 0,
        totalPage: data.totalPage || 0
      }
    },
    mounted () {
      this.getDataList()
    },
    methods: {
      // 显示添加角色弹窗
      showAddRoleForm () {
        this.addDialog = true
      },
      // 分配权限
      allot (role) {
        this.allotRoleDialog = true
      },

      // 删除
      rmRole (role) {
        this.$confirm('是否删除当前权限', '', {
          confirmButtonClass: 'el-button--danger',
          confirmButtonText: '确认',
          cancelButtonText: '取消'
        }).then(() => {
          alert('shanchu ')
        })
      },

      /**
       * 获取数据
       */
      getDataList (arg = {}) {
        let params = Object.assign({
          ...this.formInline,
          currentPage: this.currentPage
        }, arg)
        console.log(params)
        // @TODO axios.get()
      },

      /**
       * 搜索数据
       */
      search () {
        this.currentPage = 1
        this.getDataList()
      },
      /**
       * 当前页码改变
       */
      onCurrentChange (page) {
        this.currentPage = page
        this.getDataList()
      },
      /* 新增角色确定按钮 */
      okBtn() {
        this.$refs['form'].validate((valid) => {
          if(valid) {

          } else {

          }
        })
      },
    }
  }
</script>
